/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponse;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.BindUserInfoDto;
import com.yami.shop.distribution.comment.model.DistributionRecruitSet;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.service.DistributionBindUserService;
import com.yami.shop.distribution.comment.service.DistributionRecruitSetService;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author yami
 */
@RestController
@RequestMapping("/p/distribution")
@Api(tags = "分销员绑定用户")
@AllArgsConstructor
@Slf4j
public class DistributionBindUserController {

    private final DistributionBindUserService distributionBindUserService;

    private final DistributionUserService distributionUserService;

    private final DistributionRecruitSetService distributionRecruitSetService;

    @PostMapping("/bindUser")
    @ApiOperation(value = "绑定用户", notes = "根据分销员卡号绑定用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNo", value = "卡号", required = true, dataType = "String")
    })
    public ResponseEntity<Void> bindUser(@RequestBody String cardNo) {

        String userId = SecurityUtils.getUser().getUserId();

        log.info("绑定用户cardNo:{}",cardNo);

        if (StrUtil.isBlank(cardNo)) {
            log.error("获取推广员信息失败");
            return ResponseEntity.ok().build();
        }

        DistributionUser sharerUser = distributionUserService.getByCardNo(cardNo);

        if (sharerUser == null) {
            log.error("获取推广员信息失败");
            return ResponseEntity.ok().build();
        }
        if (sharerUser.getState() != 1) {
            log.error("推广员状态异常");
            return ResponseEntity.ok().build();
        }
        Long shopId = sharerUser.getShopId();

        DistributionRecruitSet distributionRecruitSet = distributionRecruitSetService.getByShopId();

        if (distributionRecruitSet.getState() == 0) {
            log.error("推广计划已关闭");
            return ResponseEntity.ok().build();
        }


        ServerResponse<DistributionUser> serverResponse = distributionBindUserService.bindDistribution(sharerUser, userId, 0);
        if (!serverResponse.isSuccess()) {
            log.error(serverResponse.getMsg());
        }
        return ResponseEntity.ok().build();
    }


    @GetMapping("/bindUserList")
    @ApiOperation(value = "绑定用户列表", notes = "获取分销员所绑定的用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true, dataType = "String")
    })
    public ResponseEntity<IPage<BindUserInfoDto>> bindUserList(PageParam pageParam) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        String userId = SecurityUtils.getUser().getUserId();

        return ResponseEntity.ok(distributionBindUserService.bindUserList(pageParam,shopId,userId));
    }
}
